package com.lgh.springframework.beans.factory.support;

import com.lgh.springframework.beans.BeansException;
import com.lgh.springframework.beans.factory.BeanFactory;
import com.lgh.springframework.beans.factory.config.BeanDefinition;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname AbstractBeanFactory
 * @description 模板方法定义bean的获取
 * @date 2023/02/21 20:55
 * <p>
 * ******************************************************
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        Object singleBean = getSingleBean(name);
        if (singleBean != null) {
            return singleBean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    /**
     * 创建bean对象
     * @param beanName
     * @param beanDefinition
     * @return
     * @throws BeansException
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

    /**
     * 获取bean定义对象
     * @param beanName
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
}
